package com.hudongwx.sadpw.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.app.LoaderManager.LoaderCallbacks;

import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.AsyncTask;

import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import com.hudongwx.sadpw.MyApplication;
import com.hudongwx.sadpw.MyJsonObject;
import com.hudongwx.sadpw.R;
import com.hudongwx.sadpw.utils.API;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import static android.Manifest.permission.READ_CONTACTS;

/**
 * A login screen that offers login via email/password.
 */
@ContentView(R.layout.activity_login)
public class LoginActivity extends AppCompatActivity{
    @ViewInject(R.id.login_yzm)
    TextView tv;
    @ViewInject(R.id.login_phone)
    EditText et;
    @ViewInject(R.id.phoneNum)
            TextView tv1;
    @ViewInject(R.id.YunMa)
            TextView tv2;
    @ViewInject(R.id.login_register)
            TextView tv3;
    @ViewInject(R.id.login_back)
            ImageView iv;
    @ViewInject(R.id.login_btn)
    Button button;
    @ViewInject(R.id.login_phone)
            EditText acc;
    @ViewInject(R.id.password)
            EditText pwd;
    JSONObject data;
    String TAG="LoginActivity";
    View view;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        loginPhone(view);
        dianJi(view);
        tv3.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
                startActivity(intent);
            }
        });
        iv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public void loginPhone(View view) {
        tv1.setTextColor(android.graphics.Color.parseColor("#ffaa00"));
        tv2.setTextColor(Color.GRAY);
        et.setHint("手机号");
        tv.setVisibility(View.VISIBLE);
    }

    public void loginYunma(View view) {
        tv2.setTextColor(android.graphics.Color.parseColor("#ffaa00"));
        tv1.setTextColor(Color.GRAY);
        et.setHint("云码");
        tv.setVisibility(View.GONE);
    }
    public void dianJi(View view){
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               if (TextUtils.isEmpty(acc.getText())&&TextUtils.isEmpty(pwd.getText())){
                   Toast.makeText(LoginActivity.this,"账户或密码不能为空",Toast.LENGTH_SHORT).show();
               }else {
                   RequestParams params=new RequestParams(API.loginUrl);
                   params.addBodyParameter("Action","usePwd");
                   params.addBodyParameter("Md5Pwd",md5(pwd.getText().toString()).toUpperCase());
                   params.addBodyParameter("Phone",acc.getText().toString());
                   x.http().post(params, new Callback.CommonCallback<MyJsonObject>() {
                       @Override
                       public void onSuccess(MyJsonObject result) {
                           try {

                               Log.i(TAG, "onSuccess: result"+result.toString());
                               MyApplication.Uid=result.getString("Status");
                               data = result.getJSONObject("Data");
                               Toast.makeText(LoginActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();
                               MyApplication.saveSta(MyApplication.Uid);
                           }
                           catch (JSONException e) {
                               e.printStackTrace();
                           }
                       }

                       @Override
                       public void onError(Throwable ex, boolean isOnCallback) {

                       }

                       @Override
                       public void onCancelled(CancelledException cex) {

                       }

                       @Override
                       public void onFinished() {

                       }
                   });
               }
            }
        });
    }
    public String md5(String str){
        if (TextUtils.isEmpty(str)){
            return "";
        }
        MessageDigest messageDigest=null;
        try {
            messageDigest=MessageDigest.getInstance("MD5");
            byte[]bytes=messageDigest.digest(str.getBytes());
            String result="";
            for (byte  b: bytes) {
                String temp=Integer.toHexString(b &0xff);
                if (temp.length()==1){
                    temp="0"+temp;
                }
                result+=temp;
            }
            return  result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }
}

